import java.util.HashSet;

//TODO: Optimize so that the algoritm doesn't take forever to complete
public class MarioGraph {
	
	//Maximum nodes to check for. Plays a big part in optimazation
	private static int maxSteps = 0;
	
	//The nodes of the graph
	private static HashSet<Move> nodes = new HashSet<Move>();
	//The winner Node
	private static Move winnerNode;

	/**
	 * Add a node to the graph
	 * @param node
	 */
	public static void addNode(Move node) {

		//Forget if steps are way too high
		if (maxSteps != 0) {
			if (node.steps > maxSteps) return;
		}
		
		Move nodeToDelete = null;
		
		//Check all nodes for same values
		for (Move n : nodes) {
			//If there is similar node
			if (n.isTheSame(node)) {
				if (node.steps < n.steps) {
					//This way is faster
					nodeToDelete = n;
				}else{
					//This way is slower
					return;
				}
			}
		}
		
		//Delete the old node that is the same, but slower than the new node
		if (nodeToDelete != null) {
			nodes.remove(nodeToDelete);
		}
		
		//System.out.println(node);
		nodes.add(node);
		node.addAdjacent();
	}
	
	/**
	 * Add a winning node to the graph
	 * @param node
	 */
	public static void addWinnerNode(Move node) {

		if (winnerNode == null) {
			winnerNode = node;
		}else{
			if (node.steps < winnerNode.steps) {
				winnerNode = node;
			}else{
				return;
			}
		}
		maxSteps = winnerNode.steps; //Simple optimization: Don't check what is already too slow.
	}
	
	/**
	 * Print the amount of moves it takes to win
	 */
	public static void printWinner() {
		System.out.println();
		if (winnerNode == null) {
			System.out.println("Mario could not reach exit");
		}else{
			System.out.println(winnerNode.steps);
		}
	}
}
